package com.yami.shop.admin.controller;

import com.yami.shop.bean.model.Card;
import com.yami.shop.service.CardService;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

@Controller
@RequestMapping("/card")
public class CardController {
    @Autowired
    private CardService cardService;

    @PostMapping("/verifyCard")
    public ResponseEntity<String> updateCard(Card card){
        //获取账户,查询有没有对应的账户信息
        StringBuffer requestUrl = new StringBuffer("http://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=");
        requestUrl.append("42050164654300000207").append("&cardBinCheck=true");
        HttpClient client = HttpClientBuilder.create().build();//构建一个Client
        HttpGet get = new HttpGet(requestUrl.toString());//构建一个POST请求
        HttpResponse response = null;//提交POST请求
        String content =null;
        try {
            response = client.execute(get);
            HttpEntity result = response.getEntity();
            content = EntityUtils.toString(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResponseEntity.ok(content);
    }
}
